﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage;

namespace DevToolBox.Cloud.Azure
{
    public class DictionaryTableEntity : TableEntity, IDictionary<string, EntityProperty>
    {
        private IDictionary<string, EntityProperty> _properties;

        public DictionaryTableEntity()
        {
            _properties = new Dictionary<string, EntityProperty>();
        }

        public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
        {
            this._properties = properties;
        }

        public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
        {
            return _properties;
        }

        public void Add(string attName, object attValue, DbType attType)
        {
            switch (attType)
            {
                case DbType.Boolean:
                    var boolVal = Convert.ToBoolean(attValue);
                    _properties.Add(attName, new EntityProperty(boolVal));
                    break;
                case DbType.Byte:
                    var byteVal = (byte[]) attValue;
                    _properties.Add(attName, new EntityProperty(byteVal));
                    break;
                case DbType.DateTime:
                    var dtVal = Convert.ToDateTime(attValue);
                    _properties.Add(attName, new EntityProperty(dtVal));
                    break;
                case DbType.DateTimeOffset:
                    var dtOffSetVal = (DateTimeOffset) attValue;
                    _properties.Add(attName, new EntityProperty(dtOffSetVal));
                    break;
                case DbType.Double:
                    var doubleVal = Convert.ToDouble(attValue);
                    _properties.Add(attName, new EntityProperty(doubleVal));
                    break;
                case DbType.Guid:
                    var guidVal = (Guid) attValue;
                    _properties.Add(attName, new EntityProperty(guidVal));
                    break;
                case DbType.Int32:
                    var intVal = Convert.ToInt32(attValue);
                    _properties.Add(attName, new EntityProperty(intVal));
                    break;
                case DbType.Int64:
                    var longVal = Convert.ToInt64(attValue);
                    _properties.Add(attName, new EntityProperty(longVal));
                    break;
                case DbType.String:
                    var strVal = Convert.ToString(attValue);
                    _properties.Add(attName, new EntityProperty(strVal));
                    break;
            }

        }

        public bool ContainsKey(string key)
        {
            return _properties.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return _properties.Keys; }
        }

        public bool Remove(string key)
        {
            return _properties.Remove(key);
        }

        public bool TryGetValue(string key, out EntityProperty value)
        {
            return _properties.TryGetValue(key, out value);
        }

        public ICollection<EntityProperty> Values
        {
            get { return _properties.Values; }
        }

        public EntityProperty this[string key]
        {
            get { return _properties[key]; }
            set { _properties[key] = value; }
        }

        public void Add(KeyValuePair<string, EntityProperty> item)
        {
            _properties.Add(item);
        }

        public void Clear()
        {
            _properties.Clear();
        }

        public bool Contains(KeyValuePair<string, EntityProperty> item)
        {
            return _properties.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, EntityProperty>[] array, int arrayIndex)
        {
            _properties.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _properties.Count; }
        }

        public bool IsReadOnly
        {
            get { return _properties.IsReadOnly; }
        }

        public bool Remove(KeyValuePair<string, EntityProperty> item)
        {
            return _properties.Remove(item);
        }

        public IEnumerator<KeyValuePair<string, EntityProperty>> GetEnumerator()
        {
            return _properties.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _properties.GetEnumerator();
        }

        public void Add(string key, EntityProperty value)
        {
            _properties.Add(key, value);
        }
    }
}
